<template>
	<view class="layout">
		<wd-navbar title="出入库管理" left-text="返回" @click-left="uni.navigateBack" left-arrow />
		<wd-card title="功能选择">
			<wd-grid border clickable :column="2">
			<wd-grid-item 
				icon="add-circle" 
				text="成品入库" 
				@itemclick="navigateToScan"
			/>
			<wd-grid-item 
				icon="minus-circle" 
				text="成品出库" 
				@itemclick="navigateToOutBound"
			/>
		<wd-grid-item 
			icon="read" 
			text="入库信息" 
			@itemclick="navigateToInfo"
		/>
		<wd-grid-item 
			icon="goods" 
			text="出库信息" 
			@itemclick="navigateToOutboundInfo"
		/>
		<wd-grid-item 
			icon="chart" 
			text="库存信息" 
			@itemclick="navigateToInventoryInfo"
		/>
			</wd-grid>
		</wd-card>
	</view>
</template>

<script setup>
	// 移除了 onMounted, onUnmounted 的导入，因为不再需要PDA扫码监听
	
	// 跳转到成品入库扫码页面
	const navigateToScan = () => {
		uni.navigateTo({
			url: '/pages/store_scan/store_scan'
		});
	};
	
	// 跳转到成品出库页面
	const navigateToOutBound = () => {
		uni.navigateTo({
			url: '/pages/out_bound/out_bound'
		});
	};
	
	// 处理扫码结果的统一函数
	const handleScanResult = (qrContent) => {
		const parts = qrContent.split('_zjhy_');
		
		if (parts.length >= 4) {
			const processCardId = parts[0]; // 流程卡ID
			const smallRollSpecId = parts[2]; // 小卷规格ID
			
			// 跳转到入库信息页面，并传递参数
			const query = `processCardId=${processCardId}&smallRollSpecId=${smallRollSpecId}`;
			uni.navigateTo({
				url: `/pages/store_info/store_info?${query}`
			});
		} else {
			uni.showToast({
				title: '二维码格式不正确',
				icon: 'error'
			});
		}
	};
	
	// 点击按钮扫码查询入库信息
	const navigateToInfo = () => {
		uni.scanCode({
			onlyFromCamera: true,
			success: function (res) {
				handleScanResult(res.result);
			},
			fail: function (err) {
				uni.showToast({
					title: '扫码失败',
					icon: 'error'
				});
			}
		});
	};
	
	// 处理出库信息扫码结果
	const handleOutboundScanResult = (qrContent) => {
		const parts = qrContent.split('_zjhy_');
		
		if (parts.length >= 4) {
			const smallRollId = parts[2]; // 小卷ID
			
			// 跳转到出库信息页面，并传递参数
			uni.navigateTo({
				url: `/pages/outbound_info/outbound_info?smallRollId=${smallRollId}`
			});
		} else {
			uni.showToast({
				title: '二维码格式不正确',
				icon: 'error'
			});
		}
	};
	
	// 点击按钮扫码查询出库信息
	const navigateToOutboundInfo = () => {
		uni.scanCode({
			onlyFromCamera: true,
			success: function (res) {
				handleOutboundScanResult(res.result);
			},
			fail: function (err) {
				uni.showToast({
					title: '扫码失败',
					icon: 'error'
				});
			}
		});
	};
	
	// 跳转到库存信息页面
	const navigateToInventoryInfo = () => {
		uni.navigateTo({
			url: '/pages/inventory_info/inventory_info'
		});
	};
	
	// 移除了PDA按键扫码功能，只保留点击按钮的相机扫码
</script>

<style scoped>
	.layout {
		padding: 16rpx;
	}
</style>